package com.sk.rpcAssist.provideAssist;

import com.sk.rpcAssist.dataManager.ProviderDataManager;
import com.sk.rpcAssist.domain.BasicConfig;
import com.sk.rpcAssist.domain.ProvideInfoValue;
import com.sk.rpcAssist.io.protocol.MessageProtocolInter;
import com.sk.rpcAssist.io.protocol.RpcRequest;
import com.sk.rpcAssist.io.socket.ClientSocket;
import com.sk.rpcAssist.io.socket.ServerSockets;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

/**
 * description：服务提供者默认策略
 *
 * @author 孙文斌
 * @Date: 2021/2/18 16:20
 */
public class DefaultProviderAstStrategy implements ProviderAssistStrategyInter {

    private ProviderDataManager providerDataManager;

    private ServerSockets serverSockets;

    private MessageProtocolInter messageProtocol;


    private final String RPC_SERVER_SOCKET_PORT = "rpc.server.socket.port";

    public DefaultProviderAstStrategy(ProviderDataManager providerDataManager,MessageProtocolInter messageProtocol) {
        this.providerDataManager = providerDataManager;
        this.messageProtocol = messageProtocol;
        //开启注册
        register();
        //启动监听处理请求
        doProcess();
    }

    @Override
    public void register() {
        providerDataManager.register();
    }

    @Override
    public void doProcess() {
        YamlPropertiesFactoryBean yamlMapFactoryBean = new YamlPropertiesFactoryBean(); //可以加载多个yml文件
        yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
        Properties properties = yamlMapFactoryBean.getObject(); //获取yml里的参数
        int port = Integer.valueOf(properties.getProperty(RPC_SERVER_SOCKET_PORT));
        this.serverSockets = new ServerSockets(port,messageProtocol);
        serverSockets.doStart();
    }
}
